package com.cocola.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    //插入时的填充策略
    // ==>  Preparing: INSERT INTO user ( name, age, email, create_time, update_time ) VALUES ( ?, ?, ?, ?, ? )
    @Override
    public void insertFill(MetaObject metaObject) {
      this.setFieldValByName("createTime", new Date(), metaObject);
      this.setFieldValByName("updateTime", new Date(), metaObject);
    }

    //更新时的填充策略
    // ==>  Preparing: UPDATE user SET name=?, update_time=? WHERE id=?
    @Override
    public void updateFill(MetaObject metaObject) {

        this.setFieldValByName("updateTime", new Date(), metaObject);
    }
}
